LitePal 基本概念
LitePal 是开源的 Android 数据库框架, 采用了 对象关系映射 ORM 模式 .
封装了常用数据库功能
ORM(对象关系映射) 指的是 面向对象语言 和 关系型数据库 之间建立一种映射关系.
使用方法
配置
1 | dependencies { |
1 | android:name="org.litepal.LitePalApplication" |
litepal.xml
1 | Connector.getDatabase() |
SQLite 简介
SQLiteOpenHelper 抽象类
{
onCreate()
onUpgrade()
getReadableDatabase() //以只读的方式打开db
getWritableDatabase() //以可读写的方式打开db,如果db只读,则返回异常
SQLiteOpenHelper() //构造方法
// 参数1 Context 参数2 数据库名 参数3 查询数据的时候返回自定义的cursor(null) 参数4 当前数据库的版本号
}
文件保存在 /data/data/<package name>/databases/
目录下
SQL 基本操作
建表 Create
1 | create table Book ( |
1 | public class MyDatabaseHelper extends SQLiteOpenHelper { |
实例化抽象类 MyDatabaseHelper1
2
3
4
5
6
7
8
9
10private MyDatabaseHelper dbHelper;
dbHelper = new MyDatabaseHelper(this, "BookStore.db",
null, 1);
Button createDatabase = findViewById(R.id.create_database);
createDatabase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper.getWritableDatabase();
}
});
删表 Drop
1 | db.execSQL("drop table if exists Book"); |
升级 Update
第四个参数, 比之前大.1
2dbHelper = new MyDatabaseHelper(this, "BookStore.db",
null, 2);
查表
.table //查表
.schema //查看建表语句
.exit .quit //退出
数据库操作 CRUD
添加数据 insert()
insert()
参数1 表名
参数2 未指定添加数据的情况下,给可为空的列自动赋值NULL. 不用就填null
参数3 ContentValues 对象, 提供了 put()方法的重载, 用于添加数据1
2
3
4
5
6
7
8
9
10SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
// 开始组装第一条数据
values.put("name", "The Da Vinci Code");
values.put("author", "Dan Brown");
values.put("pages", 454);
values.put("price", 16.94);
// 插入第一条数据
db.insert("Book", null, values);
values.clear();
修改数据 update()
update()
参数1 表名
参数2 ContentValues 对象, 提供了 put()方法的重载, 用于添加数据
参数3 4 约束更新某一行或几行 , 默认是更新全部 ; 参数3 对应 where 语句1
2
3
4
5SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("price", 10.99);
db.update("Book", values,
"name = ?", new String[] {"The Da Vinci Code"});
删除数据 deleted()
delete()
参数 1 表名
参数 2 3 whereClause 查询语句 , 比如 pages > ? , new Sting[] {“500”}1
2
3SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("Book", "pages > ?",
new String[] {"500"});
查询数据 query()
query()
参数 1 table 表名 , from table_name
参数 2 columns 查询哪几列 , select column1,column2
参数 3 selection 4 selectionArgs 约束条件 where column = value
参数 5 groupBy 的列 , group by column
参数 6 having , group by 的约束 , having column = value
参数 7 orderBy , 指定查询的排列方式 , order by column1 column2
返回 Cursor 对象1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("Book", null,null,null,null,null,null);
if(cursor.moveToFirst()){
do {
//遍历 Cursor 对象, 取出数据并打印
String name = cursor.getString(cursor.getColumnIndex("name"));
String author = cursor.getString(cursor.getColumnIndex("author"));
int pages = cursor.getInt(cursor.getColumnIndex("pages"));
double price = cursor.getDouble(cursor.getColumnIndex("price"));
Log.d(TAG,"book name is " + name);
Log.d(TAG,"book author is " + author);
Log.d(TAG,"book pages is " + pages);
Log.d(TAG,"book price is " + price);
} while(cursor.moveToNext());
}
cursor.close();
直接使用 SQL 操作数据
execSQL 和 rawQuery
1 | # 增 |